<?php
namespace app\channel\controller\sys_admin;
use app\AdminController;

use app\channel\model\ProxyUsersModel;
use app\channel\model\ProxyLevelModel;
use app\channel\model\ProxyWarrantLogModel;
use app\channel\model\TeamModel;
use app\member\model\UsersModel;
use app\member\model\UserAddressModel;
/**
 * 团队架构
 * Class Index
 * @package app\store\controller
 */
class TeamTree extends AdminController
{
	/*------------------------------------------------------ */
	//-- 优先执行
	/*------------------------------------------------------ */
	public function initialize(){
        parent::initialize();
        $this->Model = new ProxyUsersModel();
    }
	/*------------------------------------------------------ */
	//-- 主页
	/*------------------------------------------------------ */
    public function index(){
        $this->assign('isTeamTree',1);
        $this->assign('teamCount',$this->Model->count());
		return $this->fetch();
	}
    /*------------------------------------------------------ */
    //-- 获取代理列表
    /*------------------------------------------------------ */
    public function getTeamList(){
        $pid = input('pid',0,'intval');
        $rows = $this->Model->alias('pu')->join("users u", 'pu.user_id=u.user_id', 'left')->where('pu.pid',$pid)->field('u.user_id,u.real_name')->select()->toArray();
        $data = [];
        $TeamModel = new TeamModel();
        foreach ($rows as $key=>$row){
            $arr = [];
            $teamCount = $TeamModel->teamAllCount($row['user_id']);
            $arr['name'] = $row['user_id'].'-'.$row['real_name'].'('.$teamCount.')';
            $arr['type'] = 'folder';
            $arr['additionalParameters'] = ['user_id'=>$row['user_id']];
            $data[] = $arr;
        }
        return $this->success('ok','',$data);
    }
    /*------------------------------------------------------ */
    //-- 获取代理信息
    /*------------------------------------------------------ */
    public function info(){
        $user_id = input('user_id/d');
        if ($user_id < 1) return $this->error('获取用户ID传值失败！');
        $row = $this->Model->info($user_id);
        if (empty($row)) return $this->error('用户不存在！');
        $row['user_address'] = (new UserAddressModel)->where('user_id', $user_id)->select()->toArray();
        $this->assign('row', $row);
        $ProxyLevelModel = new ProxyLevelModel();
        $this->assign("proxyLevel", $ProxyLevelModel->getList());

        $this->assign('statusArr',$this->Model->statusArr);
        $this->assign('regionProxy',$this->Model->regionProxy);
        $warrantLog = (new ProxyWarrantLogModel)->where('user_id', $user_id)->order('log_id DESC')->select()->toArray();
        $this->assign('warrantLog',$warrantLog);
        $this->assign('checkIDCardStatus',UsersModel::$checkIDCardStatus);
        $this->assign('isTeamTree',1);
        $data['content'] = $this->fetch('sys_admin/users/user_info')->getContent();
        return $this->success('','',$data);
    }
}
